package com.drinks.yiniang.adapter.user.comments;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.comment.GoodsCommentJson;
import com.drinks.yiniang.entity.common.RadarEntity;
import com.drinks.yiniang.entity.user.order.OrderEvaluateJson;
import com.drinks.yiniang.ui.widget.layout.DimensionLayout;
import com.drinks.yiniang.ui.widget.layout.DimensionWrapLayout;
import com.drinks.yiniang.ui.widget.view.RadarMapView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.ui.widget.ratingbar.CustomRatingBar;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论列表adapter
 * @author han
 * @date 2019/8/30
 */
public class GoodsCommentsAdapter extends BaseQuickRCVAdapter<GoodsCommentJson.GoodsCommentList, BaseViewHolder> {
    private ViewGroup.LayoutParams paramsField;
    //private RadarMapView mRadarMapView;
    private DimensionWrapLayout mDimensionLayout;
    private ImageView mRadarImageView;

    private Drawable mCheckedDrawable;
    private Drawable mNormalDrawable;

    private TextView mTvLike;

    private List<RadarEntity> mRadarList;
    private RadarEntity mRadarEntity;

    private int dp8;
    private int dp20;

    public GoodsCommentsAdapter(Context mContext, @Nullable List<GoodsCommentJson.GoodsCommentList> data) {
        super(R.layout.item_goods_comments, data);

        this.mContext = mContext;

        mCheckedDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_comment_praise_like);
        mNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_comment_praise_normal);

        dp8 = ScreenSizeUtils.dp2px(mContext, 8);
        dp20 = ScreenSizeUtils.dp2px(mContext, 20);

        paramsField = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, dp20);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsCommentJson.GoodsCommentList item) {
        //头像
        GlideUtils.loadAvatar(mContext, (CircleImageView) helper.getView(R.id.iv_comments_avatar), item.getAvatar());
        //昵称
        helper.setText(R.id.tv_comments_nickname, item.getNickname());
        //评分
        CustomRatingBar ratingBar = helper.getView(R.id.rating_comments);
        ratingBar.setStar(item.getScore());
        //评论内容
        helper.setText(R.id.tv_comments_content, item.getContent());

        if(CommonUtils.getListSize(item.getRadar()) >= 5) {//专家点评
            helper.setGone(R.id.tv_comments_time, false);
            helper.setGone(R.id.tv_comments_like, false);

            addFields((AutoLineFeedLayout) helper.getView(R.id.ll_comments_field), item.getSkill());
            //雷达图
            setRadarData(helper, item.getRadar(), item.getRadar_map_url());
        } else {
            helper.setGone(R.id.tv_comments_time, true);
            helper.setGone(R.id.tv_comments_like, true);

            helper.setGone(R.id.tv_comments_field, false);
            helper.setGone(R.id.ll_comments_field, false);

            helper.setGone(R.id.iv_comments_radar_map, false);
            helper.setGone(R.id.lv_comments_star, false);

            //时间
            helper.setText(R.id.tv_comments_time, item.getCreated_at());
            //点赞
            helper.setText(R.id.tv_comments_like, StringUtils.getNullEmptyConvertZero(item.getLike_count()));

            //当前用户是否点赞
            mTvLike = helper.getView(R.id.tv_comments_like);
            if(Constants.STATUS_GLOBAL_YES.equals(item.getIs_like())) {
                mTvLike.setCompoundDrawablesWithIntrinsicBounds(null, null, mCheckedDrawable,null);
                mTvLike.setSelected(true);
            } else {
                mTvLike.setCompoundDrawablesWithIntrinsicBounds(null, null, mNormalDrawable,null);
                mTvLike.setSelected(false);
            }
        }

        //add click
        helper.addOnClickListener(R.id.tv_comments_like);
    }

    //添加领域
    private void addFields(AutoLineFeedLayout feedLayout, List<String> list) {
        feedLayout.removeAllViews();

        if(CommonUtils.isEmptyList(list)) {
            return;
        }

        //动态添加属性值
        for (int i = 0; i < list.size(); i++) {
            final AppCompatTextView tvAttrsValueView = getAttrsTextView();

            tvAttrsValueView.setText(list.get(i));

            feedLayout.addView(tvAttrsValueView, paramsField);
        }
    }

    private void setRadarData(BaseViewHolder helper,  List<GoodsCommentJson.GoodsCommentRadar> radarList, String radarUrl) {
        mRadarImageView = helper.getView(R.id.iv_comments_radar_map);
        mDimensionLayout = helper.getView(R.id.lv_comments_star);

        helper.setGone(R.id.tv_comments_field, true);
        helper.setGone(R.id.ll_comments_field, true);

        helper.setGone(R.id.iv_comments_radar_map, true);
        helper.setGone(R.id.lv_comments_star, true);

        mRadarList = new ArrayList<>();

        for(GoodsCommentJson.GoodsCommentRadar radar : radarList) {
            mRadarEntity = new RadarEntity(radar.getName(), StringUtils.getFloat(radar.getValue()));
            mRadarList.add(mRadarEntity);
        }

        //mRadarMapView.setRadarEvaluateData(mRadarList, 5);

        GlideUtils.loadSquareImage(mContext, mRadarImageView, radarUrl);

        //雷达图对应的星级
        mDimensionLayout.addGoodsCommentsDimensionLayout(radarList);
    }

    private AppCompatTextView getAttrsTextView() {
        AppCompatTextView tv = new AppCompatTextView(mContext);

        tv.setTextColor(ColorUtils.getColorById(mContext, R.color.color_ef3024));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        tv.setBackgroundResource(R.drawable.shape_bkg_specialist_field);
        tv.setPadding(dp8, 0, dp8, 0);
        tv.setGravity(Gravity.CENTER_VERTICAL);

        return tv;
    }
}
